﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace timetableasy.Models
{
    partial class Period
    {
        public bool IsValid
        {
            get
            {
                return (GetRuleViolations().Count() == 0);
            }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {

            if (StudyTime == null)
                yield return new RuleViolation("A StudyTime is required", "StudyTime");

            if (String.IsNullOrEmpty(PeriodName))
                yield return new RuleViolation("PeriodName is required", "PeriodName");
            if (StartDate == null)
                yield return new RuleViolation("A Start date is required", "StartDate");
            if (EndDate == null)
                yield return new RuleViolation("A End date is required", "EndDate");

            yield break;
        }

        partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            if (!IsValid)
                throw new ApplicationException("Rule violations prevent saving");
        }
    }
}